<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class ExamController extends BaseController {

//=========================线上考试跟踪================================

    public function index_online() {
        //筛选条件
        $data = array_filter($_GET);
        if (!empty($_GET['user_name'])) {
            $data['user_name'] = array('like',"%".trim($_GET['user_name'])."%");
        }
        if (!empty($_GET['user_phone'])) {
            $data['user_phone'] = array('like',"%".trim($_GET['user_phone'])."%");
        }
        unset($data['p']);
        $data['exam_midiclub_id'] = -1;

        $count = M('exam')->field('e.*,u.user_name,u.user_phone,t.*')->alias('e')->join('left join midi_user as u on e.exam_user_id = u.user_id')->join('left join midi_examtime as t on e.exam_time = t.examtime_id')->where($data)->count();
        $page = getpage($count,10);
        $exam_arr = M('exam')->field('e.*,u.user_name,u.user_phone,t.*')->alias('e')->join('left join midi_user as u on e.exam_user_id = u.user_id')->join('left join midi_examtime as t on e.exam_time = t.examtime_id')->where($data)->order('exam_id desc')->limit($page->firstRow, $page->listRows)->select();
        $this->assign('exam_arr', $exam_arr);
        $this->assign('page', $page->show());
        $this->display();
    }

    //考试详情
    public function online_details(){
        $data['exam_id'] = I('get.exam_id');
        $exam_arr = M('exam')->alias('e')->join('left join midi_user as u on e.exam_user_id = u.user_id')->join('left join midi_midiclub as m on e.exam_midiclub_id = m.midiclub_id')->join('left join midi_examtime as t on e.exam_time = t.examtime_id')->where($data)->find();
        $where['exam_examiner_id'] = $exam_arr['exam_examiner_id'];
        $examiner_arr = M('exam')->field('u.user_id,u.user_name,u.user_phone')->alias('e')->join('left join midi_user as u on e.exam_examiner_id = u.user_id')->where($where)->find();
        $this->assign('exam_arr',$exam_arr);
        $this->assign('examiner_arr',$examiner_arr);
        //回看地址
        $video_arr = array();
        $vod = getVod();
        $package = array('fileName' => $data['exam_id']);
        $vod_arr = $vod->DescribeVodPlayInfo($package);
        if (empty($vod_arr)) {
            $this->assign('video_status','获取视频失败');
        }else{
            foreach ($vod_arr['fileSet'] as $k => $v) {
                if ($v['status'] == 2) {
                    $arr = array();
                    $arr['fileName'] = $v['fileName'];
                    $arr['filePath'] = $v['playSet'][2]['url'];
                    $video_arr[] = $arr;
                }
            }
            $this->assign('video_status',$vod_arr['codeDesc']);
        }
        $this->assign('video_arr',$video_arr);
        // $url = "https://open.intviu.cn/v1/live/get_lookback_url?room_name=".$exam_arr['exam_room_id'];
        // $result = curl_get_contents($url);
        // $result_arr = json_decode($result,true);
        // $this->assign('lookback_msg',$result_arr['message']);
        // $url_arr = $result_arr['data']['lookback_url'];
        // $this->assign('lookback_urls',$url_arr);
        $this->display();
    }

    //删除线上考试
    public function examonline_del(){
        $data['exam_id'] = I('get.exam_id');
        if (M('exam')->where($data)->delete()) {
            $this->redirect('index_online');
        }else{
            $this->error('删除考试失败！');
        }
    }

//=========================线下考试================================

    public function index_offline() {
        //筛选条件
        $data = array_filter($_GET);
        if (!empty($_GET['midiclub_name'])) {
            $data['midiclub_name'] = array('like',"%".trim($_GET['midiclub_name'])."%");
        }
        unset($data['p']);
        $data['midiclub_display'] = 1;

        $count = M('examoffline')->alias('e')->join('left join midi_midiclub as m on e.examoffline_roomid = m.midiclub_id')->where($data)->order('e.examoffline_id desc')->count();
        $page = getpage($count,10);
        $examoffline = M('examoffline')->alias('e')->join('left join midi_midiclub as m on e.examoffline_roomid = m.midiclub_id')->where($data)->order('e.examoffline_id desc')->limit($page->firstRow, $page->listRows)->select();
        $this->assign('examoffline', $examoffline);
        $this->assign('page', $page->show());
        $this->display();
    }

    public function exam_offline_add(){
        if ($_POST['action'] == 'exam_offline_add_action') {
            unset($_POST['action']);
            $this->exam_offline_add_action();
            exit();
        }
        $data['midiclub_display'] = 1;
        $data['midiclub_type'] = array('IN','2,3');
        $midiclubs = M('midiclub')->field('midiclub_id,midiclub_name')->where($data)->select();
        $this->assign('midiclubs',$midiclubs);
        $this->display();
    }

    public function exam_offline_add_action(){
        $_POST['examoffline_createtime'] = time();
        $data['examoffline_roomid'] = $_POST['examoffline_roomid'];
        $data['examoffline_ymd'] = $_POST['examoffline_ymd'];
        if (empty($data['examoffline_roomid']) || empty($data['examoffline_ymd'])) {
            $this->error('考场和考试时间不能为空');
        }
        $time = strtotime($data['examoffline_ymd']);
        $now = time();
        if ($time <= $now) {
            $this->error('考试时间必须大于当前时间');
        }
        if (M('examoffline')->where($data)->find()) {
            $this->error('考场每天只能设置一场考试');
        }
        if (M('examoffline')->add($_POST)) {
            $this->success('添加线下考试成功','index_offline');
        }else{
            $this->error('添加线下考试失败');
        }
    }

    public function offline_details(){
        $data['examoffline_id'] = I('get.id');
        $examoffline = M('examoffline')->alias('e')->join('left join midi_midiclub as m on e.examoffline_roomid = m.midiclub_id')->where($data)->find();
        $this->assign('examoffline',$examoffline);
        //电吉他考生
        $condition['exam_ymd'] = $examoffline['examoffline_ymd'];
        $condition['exam_midiclub_id'] = $examoffline['examoffline_roomid'];
        $condition['exam_status'] = 1;
        $condition['exam_major_id'] = 1;
        $guitar_arr = M('exam')->alias('e')->field('u.user_id,u.user_name,u.user_phone,exam_major_id,exam_level,exam_applytime')->join('left join midi_user as u on e.exam_user_id = u.user_id')->where($condition)->select();
        $this->assign('guitar_arr',$guitar_arr);
        //爵士鼓考生
        $condition['exam_major_id'] = 2;
        $drum_arr = M('exam')->alias('e')->field('u.user_id,u.user_name,u.user_phone,exam_major_id,exam_level,exam_applytime')->join('left join midi_user as u on e.exam_user_id = u.user_id')->where($condition)->select();
        $this->assign('drum_arr',$drum_arr);
        //电贝司考生
        $condition['exam_major_id'] = 3;
        $bass_arr = M('exam')->alias('e')->field('u.user_id,u.user_name,u.user_phone,exam_major_id,exam_level,exam_applytime')->join('left join midi_user as u on e.exam_user_id = u.user_id')->where($condition)->select();
        $this->assign('bass_arr',$bass_arr);

        $this->display();
    }

    /**
    * 数组转xls格式的excel文件
    * @param  array  $data      需要生成excel文件的数组
    * @param  string $filename  生成的excel文件名
    *      示例数据：
    *       $data = array(
    *          array(NULL, 2010, 2011, 2012),
    *           array('Q1',   12,   15,   21),
    *           array('Q2',   56,   73,   86),
    *           array('Q3',   52,   61,   69),
    *           array('Q4',   30,   32,    0),
    *          );
    */
    public function exam_offline_export(){
        $condition['examoffline_id'] = I('get.id');
        $examoffline_arr = M('examoffline')->alias('e')->join('midi_midiclub as m on e.examoffline_roomid = m.midiclub_id')->where($condition)->find();
        $data = array();
        $title = array('考场名称','考试时间','考场地址','考场电话','电吉他考试最大人数','电吉他考试已报名人数','爵士鼓考试最大人数','爵士鼓考试已报名人数','电吉他考试最大人数','电吉他考试已报名人数');
        array_push($data, $title);
        $arr = array($examoffline_arr['midiclub_name'],$examoffline_arr['examoffline_ymd'],$examoffline_arr['midiclub_address'],$examoffline_arr['midiclub_phone'],$examoffline_arr['examoffline_max_guitar'],$examoffline_arr['examoffline_already_guitar'],$examoffline_arr['examoffline_max_drum'],$examoffline_arr['examoffline_already_drum'],$examoffline_arr['examoffline_max_bass'],$examoffline_arr['examoffline_already_bass']);
        array_push($data, $arr);
        array_push($data, array());
        array_push($data, array('电吉他考生')); //电吉他考生
        $title = array('序号','迷笛ID','姓名','性别','电话','专业','等级','生日','身份证','民族','国籍','地址','报名时间','证书邮寄地址');
        array_push($data, $title);
        //电吉他考生
        $data_guitar['exam_ymd'] = $examoffline_arr['examoffline_ymd'];
        $data_guitar['exam_midiclub_id'] = $examoffline_arr['examoffline_roomid'];
        $data_guitar['exam_status'] = 1;
        $data_guitar['exam_major_id'] = 1;
        $guitar_arr = M('exam')->alias('e')->field('u.user_id,u.user_sex,u.user_name,u.user_phone,exam_major_id,exam_level,u.user_birthday,u.user_idcard,u.user_nation,u.user_country,u.user_idaddress,exam_applytime,exam_certaddress')->join('left join midi_user as u on e.exam_user_id = u.user_id')->where($data_guitar)->select();
        foreach ($guitar_arr as $k => $v) {
            $user_id = $v['user_id'];
            $user_name = $v['user_name'];
            if ($v['user_sex'] == 1) {
                $user_sex = '男';
            }elseif ($v['user_sex'] == 2) {
                $user_sex = '女';
            }
            $user_phone = $v['user_phone'].' ';
            if ($v['exam_major_id'] == 1) {
                $exam_major_id = '电吉他';
            }elseif ($v['exam_major_id'] == 2) {
                $exam_major_id = '爵士鼓';
            }elseif ($v['exam_major_id'] == 3) {
                $exam_major_id = '电贝司';
            }
            $exam_level = $v['exam_level'];
            $user_birthday = $v['user_birthday'];
            $user_idcard = $v['user_idcard'].' ';
            $user_nation = $v['user_nation'];
            $user_country = $v['user_country'];
            $user_idaddress = $v['user_idaddress'];
            $exam_certaddress = $v['exam_certaddress'];
            $exam_applytime = date('Y-m-d H:i',$v['exam_applytime']);
            $arr = array();
            $arr = array(($k+1),$user_id,$user_name,$user_sex,$user_phone,$exam_major_id,$exam_level,$user_birthday,$user_idcard,$user_nation,$user_country,$user_idaddress,$exam_applytime,$exam_certaddress);
            array_push($data, $arr);
        }

        array_push($data, array());
        array_push($data, array('爵士鼓考生')); //爵士鼓考生
        $title = array('序号','迷笛ID','姓名','性别','电话','专业','等级','生日','身份证','民族','国籍','地址','报名时间','证书邮寄地址');
        array_push($data, $title);
        //爵士鼓考生
        $data_drum['exam_ymd'] = $examoffline_arr['examoffline_ymd'];
        $data_drum['exam_midiclub_id'] = $examoffline_arr['examoffline_roomid'];
        $data_drum['exam_status'] = 1;
        $data_drum['exam_major_id'] = 2;
        $drum_arr = M('exam')->alias('e')->field('u.user_id,u.user_sex,u.user_name,u.user_phone,exam_major_id,exam_level,u.user_birthday,u.user_idcard,u.user_nation,u.user_country,u.user_idaddress,exam_applytime,exam_certaddress')->join('left join midi_user as u on e.exam_user_id = u.user_id')->where($data_drum)->select();
        foreach ($drum_arr as $k => $v) {
            $user_id = $v['user_id'];
            $user_name = $v['user_name'];
            if ($v['user_sex'] == 1) {
                $user_sex = '男';
            }elseif ($v['user_sex'] == 2) {
                $user_sex = '女';
            }
            $user_phone = $v['user_phone'].' ';
            if ($v['exam_major_id'] == 1) {
                $exam_major_id = '电吉他';
            }elseif ($v['exam_major_id'] == 2) {
                $exam_major_id = '爵士鼓';
            }elseif ($v['exam_major_id'] == 3) {
                $exam_major_id = '电贝司';
            }
            $exam_level = $v['exam_level'];
            $user_birthday = $v['user_birthday'];
            $user_idcard = $v['user_idcard'].' ';
            $user_nation = $v['user_nation'];
            $user_country = $v['user_country'];
            $user_idaddress = $v['user_idaddress'];
            $exam_certaddress = $v['exam_certaddress'];
            $exam_applytime = date('Y-m-d H:i',$v['exam_applytime']);
            $arr = array();
            $arr = array(($k+1),$user_id,$user_name,$user_sex,$user_phone,$exam_major_id,$exam_level,$user_birthday,$user_idcard,$user_nation,$user_country,$user_idaddress,$exam_applytime,$exam_certaddress);
            array_push($data, $arr);
        }

        array_push($data, array());
        array_push($data, array('电贝司考生')); //电贝司考生
        $title = array('序号','迷笛ID','姓名','性别','电话','专业','等级','生日','身份证','民族','国籍','地址','报名时间','证书邮寄地址');
        array_push($data, $title);
        //电贝司考生
        $data_bass['exam_ymd'] = $examoffline_arr['examoffline_ymd'];
        $data_bass['exam_midiclub_id'] = $examoffline_arr['examoffline_roomid'];
        $data_bass['exam_status'] = 1;
        $data_bass['exam_major_id'] = 3;
        $bass_arr = M('exam')->alias('e')->field('u.user_id,u.user_sex,u.user_name,u.user_phone,exam_major_id,exam_level,u.user_birthday,u.user_idcard,u.user_nation,u.user_country,u.user_idaddress,exam_applytime,exam_certaddress')->join('left join midi_user as u on e.exam_user_id = u.user_id')->where($data_bass)->select();
        foreach ($bass_arr as $k => $v) {
            $user_id = $v['user_id'];
            $user_name = $v['user_name'];
            if ($v['user_sex'] == 1) {
                $user_sex = '男';
            }elseif ($v['user_sex'] == 2) {
                $user_sex = '女';
            }
            $user_phone = $v['user_phone'].' ';
            if ($v['exam_major_id'] == 1) {
                $exam_major_id = '电吉他';
            }elseif ($v['exam_major_id'] == 2) {
                $exam_major_id = '爵士鼓';
            }elseif ($v['exam_major_id'] == 3) {
                $exam_major_id = '电贝司';
            }
            $exam_level = $v['exam_level'];
            $user_birthday = $v['user_birthday'];
            $user_idcard = $v['user_idcard'].' ';
            $user_nation = $v['user_nation'];
            $user_country = $v['user_country'];
            $user_idaddress = $v['user_idaddress'];
            $exam_certaddress = $v['exam_certaddress'];
            $exam_applytime = date('Y-m-d H:i',$v['exam_applytime']);
            $arr = array();
            $arr = array(($k+1),$user_id,$user_name,$user_sex,$user_phone,$exam_major_id,$exam_level,$user_birthday,$user_idcard,$user_nation,$user_country,$user_idaddress,$exam_applytime,$exam_certaddress);
            array_push($data, $arr);
        }
        $filename = $examoffline_arr['midiclub_name'].$examoffline_arr['examoffline_ymd'].'线下考试统计'.'.xls';
        create_xls($data,$filename);
    }

}